package me.wangxuan.pat.view;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.logging.Logger;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.table.DefaultTableModel;

import lombok.Data;
import lombok.EqualsAndHashCode;
import me.wangxuan.pat.entity.Account;
import me.wangxuan.pat.entity.Api;
import me.wangxuan.pat.entity.Project;

/**
 * 主页面
 * 
 * @author shawn
 *
 */

@Data
@EqualsAndHashCode(callSuper=false)
public class MainView extends JFrame {
	private static final long serialVersionUID = 1L;
	private static final Logger logger = Logger.getLogger("global");
	private JPanel asidePanel;
	private JComboBox<Project> projectBox;
	private JButton projectBtn;
	private JComboBox<Account> accountBox;
	private JButton accountBtn;
	private JList<Api> apilist;
	private JButton apiBtn;
	
	
	private ProjectManageDialog projectDialog;
	
	private JPanel mainPanel;

	private JComboBox<String> methodBox;
	private JComboBox<String> contentTypeBox;
	private JTextField urlField;
	private JButton sendBtn;
	private JCheckBox processorCheck;

	private JTable paramsTable;
	private DefaultTableModel tableModel;
	private JButton addParamsBtn;

	private JTextPane respPane;

	public MainView() {
		logger.info("MainView init");
		setTitle("支付接口工具");
		setSize(1200, 900);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(new BorderLayout());
		Container pane = getContentPane();

		initLeftPane(pane);

		initMainPane(pane);
		
		pack();
	}

	private void initLeftPane(Container pane) {
		asidePanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
//		asidePanel.add(new JLabel("Aside"));
		// 工程栏
		JPanel projectRow = new JPanel(new FlowLayout(FlowLayout.LEFT));
		projectBox = new JComboBox<>(new Project[] { });
		projectRow.add(projectBox);
		projectBtn = new JButton("管理工程");

		projectRow.add(projectBtn);

		asidePanel.add(projectRow);
		// 账号栏
		JPanel accountRow = new JPanel(new FlowLayout(FlowLayout.LEFT));

		accountBox = new JComboBox<>(new Account[] {});
		accountRow.add(accountBox);
		accountBtn = new JButton("管理账号");
		accountRow.add(accountBtn);
		asidePanel.add(accountRow);

		JPanel apiPanel = new JPanel(new FlowLayout());
		apiPanel.setLayout(new BoxLayout(apiPanel, BoxLayout.Y_AXIS));
		
		DefaultListModel<Api> listModel = new DefaultListModel<Api>();
		// 创建 JList
		apilist = new JList<>(listModel);

		// 设置 JList 的宽度
		apilist.setPreferredSize(new Dimension(150, 200)); // 设置 JList 的宽度为 150 像素,高度200不然滚动条不显示
		// 创建 JScrollPane 并添加 JList
		JScrollPane scrollPane = new JScrollPane(apilist);
		scrollPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10)); // 设置上下左右的边距

		apiPanel.add(scrollPane);
		apiBtn = new JButton("管理api");
		apiBtn.setAlignmentX(Component.CENTER_ALIGNMENT); // 水平居中对齐
		//apiPanel.add(Box.createRigidArea(new Dimension(0, 10))); // 添加垂直间隔
		apiPanel.add(apiBtn);

		asidePanel.add(apiPanel);

		asidePanel.setBorder(BorderFactory.createEtchedBorder());// 创建一个蚀刻边框
		asidePanel.setPreferredSize(new Dimension(200, this.getHeight())); // 设置asidePanel宽度为200
		pane.add(asidePanel, BorderLayout.WEST);
	}

	private void initMainPane(Container pane) {
		mainPanel = new JPanel();
		mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
		mainPanel.setBorder(BorderFactory.createEtchedBorder());// 创建一个蚀刻边框
		pane.add(mainPanel, BorderLayout.CENTER);

		// 地址栏
		JPanel addressRow = new JPanel(new FlowLayout(FlowLayout.LEFT));
		methodBox = new JComboBox<>(new String[] { "POST", "GET" });
		contentTypeBox = new JComboBox<>(new String[] {"application/x-www-form-urlencoded", "application/json","application/xml","text/plain","text/xml","text/html","application/octet-stream","text/event-stream","multipart/form-data"});
		urlField = new JTextField(30);
		urlField.setText("http://www.baidu.com");
		sendBtn = new JButton("发送");
		processorCheck = new JCheckBox("启用加验签",true);
		addressRow.add(methodBox);
		addressRow.add(contentTypeBox);
		addressRow.add(urlField);
		addressRow.add(sendBtn);
		addressRow.add(processorCheck);
		mainPanel.add(addressRow);

		JPanel paramsPanel = new JPanel(new BorderLayout());
		String[] columnNames = { "参数名", "参数值", "生成器", "操作" };
		tableModel = new DefaultTableModel(columnNames, 0);
		paramsTable = new JTable(tableModel);
		paramsTable.setRowHeight(35);
		paramsPanel.add(new JScrollPane(paramsTable), BorderLayout.CENTER);

		JPanel btnPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		addParamsBtn = new JButton("增加参数");
		btnPanel.add(addParamsBtn);
		paramsPanel.add(btnPanel, BorderLayout.SOUTH);
		mainPanel.add(paramsPanel);

		respPane = new JTextPane();
		respPane.setEditable(false); // 设置为不可编辑
		respPane.setFont(new Font("Monospaced", Font.PLAIN, 12)); // 设置字体
		// 创建 JScrollPane 包装 JTextPane
		JScrollPane scrollPane = new JScrollPane(respPane);
		scrollPane.setPreferredSize(new Dimension(700, 300)); // 设置 JScrollPane 的高度为 300
		mainPanel.add(scrollPane);
	}

}
